﻿using LD.Host;
using ReaLTaiizor.Controls;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using YTE.Control;
using 右下角弹窗.DiagShow;

namespace LD.BC
{
    public partial class FrmAxisPointEdit : FrmOKCancel
    {
        public Action<TB_Point> ActionPoint;
        private bool _edit = false;
        public TB_Point AxisPoint
        {
            get;
            private set;
        }
        public FrmAxisPointEdit()
        {
            InitializeComponent();
            this.Shown += FrmBcSettingPointDialog_Shown;
            this.btn_ok.Click += Btn_ok_Click;
            Init();

        }
        public FrmAxisPointEdit(TB_Point point) : this()
        {
            AxisPoint = point;
            if (point != null)
            {
                this.txt_name.Text = point.Name;
                this.txt_name.Enabled = false;
                _edit = true;
                foreach (var item in point.AxisPointDic)
                {
                    switch (Enum.Parse(typeof(AxisType), item.Key))
                    {
                        case AxisType.X:
                            this.cob_x.SelectedIndex = item.Value.AxisId;
                            break;
                        case AxisType.Y:
                            this.cob_y.SelectedIndex = item.Value.AxisId;
                            break;
                        case AxisType.R:
                            this.cob_r.SelectedIndex = item.Value.AxisId;
                            break;
                        case AxisType.Z:
                            this.cob_z.SelectedIndex = item.Value.AxisId;
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        private void Btn_ok_Click(object sender, EventArgs e)
        {
            Dialog dialog = new Dialog();
            string name = this.txt_name.Text.Trim();
            int x = this.cob_x.SelectedIndex == -1 ? -1 : this.cob_x.SelectedIndex;
            int y = this.cob_y.SelectedIndex == -1 ? -1 : this.cob_y.SelectedIndex;
            int r = this.cob_r.SelectedIndex == -1 ? -1 : this.cob_r.SelectedIndex;
            int z = this.cob_z.SelectedIndex == -1 ? -1 : this.cob_z.SelectedIndex;
            Dictionary<string, TB_Point_Axis> AxisDic = new Dictionary<string, TB_Point_Axis>();
            if (x >= 0)
                AxisDic.Add(AxisType.X.ToString(), new TB_Point_Axis() { AxisId = (ushort)this.cob_x.SelectedValue, AxisName = this.cob_x.Text });
            if (y >= 0)
                AxisDic.Add(AxisType.Y.ToString(), new TB_Point_Axis() { AxisId = (ushort)this.cob_y.SelectedValue, AxisName = this.cob_y.Text });
            if (r >= 0)
                AxisDic.Add(AxisType.R.ToString(), new TB_Point_Axis() { AxisId = (ushort)this.cob_r.SelectedValue, AxisName = this.cob_r.Text });
            if (z >= 0)
                AxisDic.Add(AxisType.Z.ToString(), new TB_Point_Axis() { AxisId = (ushort)this.cob_z.SelectedValue, AxisName = this.cob_z.Text });

            TB_Point axisPoint = new TB_Point()
            {
                Name = name,
                AxisPointDic = AxisDic
            };

            ActionPoint?.Invoke(axisPoint);
            if (_edit)
                this.Close();
            dialog.Success("保存成功");
        }
        private void FrmBcSettingPointDialog_Shown(object sender, EventArgs e)
        {
        }
        private void Init()
        {
            var axis = BcFactory.Instance.Config.AxisParam.Where(p => p.IsEnabled).ToList();
            var axisy = BcFactory.Instance.Config.AxisParam.Where(p => p.IsEnabled).ToList();
            var axisr = BcFactory.Instance.Config.AxisParam.Where(p => p.IsEnabled).ToList();
            var axisz = BcFactory.Instance.Config.AxisParam.Where(p => p.IsEnabled).ToList();

            cob_x.DataSource = axis;
            cob_x.DisplayMember = "Name";
            cob_x.ValueMember = "AxisId";

            cob_y.DataSource = axisy;
            cob_y.DisplayMember = "Name";
            cob_y.ValueMember = "AxisId";

            cob_z.DataSource = axisz;
            cob_z.DisplayMember = "Name";
            cob_z.ValueMember = "AxisId";

            cob_r.DataSource = axisr;
            cob_r.DisplayMember = "Name";
            cob_r.ValueMember = "AxisId";

            this.cob_x.SelectedIndex = -1;
            this.cob_y.SelectedIndex = -1;
            this.cob_r.SelectedIndex = -1;
            this.cob_z.SelectedIndex = -1;
        }
    }
}
